const app = getApp()
import {
    getInfo
} from '../../api/mch'
import {
    getTokenByCode,
    register,
    xuidByUserInfo
} from '../../api/user'
import {
    lockSysUser
} from '../../api/member'
const util = require('../../utils/util.js')

Page({
    data: {
        motto: '芸店提供技术支持',
        mch: {},
        cardType: false,
        code:"",
        SHARE_TYPE:app.SHARE_TYPE,
        Invitee:null,
        avatar:'',
        nickname:'',
        realAuth:app.globalData.nameAvatr
    },
    onLoad: function (option) {
        var _this = this;
        // 获取商户信息
        if(option.cardType != undefined){
            _this.data.cardType = option.cardType
        }
        getInfo(app.ISV_ID).then(res => {
                if (res.type == 'success') {
                    _this.data.mch = res.data
                    _this.setData({
                        mch: _this.data.mch
                    })
                    if(this.data.mch.logo == null){
                        this.data.mch.logo  =  'http://cdnx.rzico.com/upload/20200825/bbfaa50b-ab3d-4875-83ac-7c929976369e芸助手.png'
                        _this.setData({
                            mch:  this.data.mch
                        })
                    }
                }
        })
        if (app.globalData.xuid!=null) {
        xuidByUserInfo( {xuid: app.globalData.xuid}).then(res=>{
          let  Invitee =  res.data.nickname
          this.setData({
            Invitee : Invitee
          })
        })
    }
    },
    onChooseAvatar(e){
        this.selectComponent('#uploadAvatar').uploadImg([e.detail.avatarUrl]);
    },
    getNickname: function (e){
        let params = {}
        params.nickname = e.detail.value
        this.setData({
            nickname: e.detail.value,
        })
    },
    metaClick(e){
        let params = {}
        params.avatar = e.detail.images[ e.detail.images.length -1 ]
        app.globalData.userInfo.avatar = e.detail.images[ e.detail.images.length -1 ]
        this.setData({
            avatar:e.detail.images[ e.detail.images.length -1 ],
        })
    },
    passwordLogin() {
        wx.navigateTo({
            url: '/pages/login/passwordLogin',
        })

    },
    startLockSysUser() {
        lockSysUser({ mchId: app.ISV_ID, xuid: app.globalData.xuid, shopId: app.globalData.shopId }).then(res => {
        })
    },
    nicknameClick(e) {
        this.data.nickname = e.detail.value
        this.setData({
            nickname:this.data.nickname
        })
    },
    inputcodeNum(e){
        this.data.code = e.detail.value
        this.setData({
          code: this.data.code
        })
      } ,
    callRegister: function (e,userId) {
        var _this = this
        register({
            avatar:this.data.avatar,
            nickname:this.data.nickname,
            mchId: app.ISV_ID,
            uuid: app.globalData.uuid,
            encryptData: e.detail.encryptedData,
            iv: e.detail.iv,
            code: this.data.code,
            xuid:app.globalData.xuid
        }).then(
            res => {
                if (res.type == 'success') {
                    const {
                        userInfo,
                        token
                    } = res.data
                    app.globalData.token = token
                    app.globalData.userInfo = userInfo
                    app.globalData.uuid = userId
                    _this.startLockSysUser();
                    wx.navigateBack({
                        delta: 1
                    })
                    const eventChannel = _this.getOpenerEventChannel()
                    eventChannel.emit('acceptDataFromOpenedPage', {
                        cardType: _this.data.cardType
                    });
                } else {
                    wx.showToast({
                        icon: "none",
                        title: res.content
                    })
                }
            }
        ).catch(error => {
            console.log(error)
        })
    },
    getUserInfo: function (e) {
        let _this = this;
        if(e.detail.errMsg == "getPhoneNumber:fail user deny"){
            return false
        }
        if(app.globalData.nameAvatr == true){
            // if(this.data.avatar == ""){
            //     wx.showToast({
            //         icon: 'none',
            //         title: '请上传头像',
            //     })
            //     return false
            // }
            if(this.data.nickname == ""){
                wx.showToast({
                    icon: 'none',
                    title: '请输入昵称',
                })
                return false
            }
        }
        wx.login({
            success: wxres => {
                getTokenByCode({
                    mchId: app.ISV_ID,
                    auth_code: wxres.code,
                    uuid: app.globalData.uuid,
                    pluginId: 'weixinMiniAuthPlugin'
                }).then(
                    res => {
                        console.log(res)
                        if (res.type == 'success') {
                            _this.callRegister(e,res.data.userId)
                        } else {
                            wx.showToast({
                                icon: "none",
                                title: res.content
                            })
                        }
                    }
                ).catch(error => {
                    console.log(error)
                })
            }
        })
    },
    backClick(){
        wx.navigateBack({
            delta: 1
        })
    }
})
